package com.arpa.oms.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.util.CommonUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.oms.domain.dto.OmsSaleReturnItemDTO;
import com.arpa.oms.domain.entity.OmsSaleReturnItem;
import com.arpa.oms.domain.vo.OmsSaleReturnItemVO;
import com.arpa.oms.mapper.OmsSaleReturnItemMapper;
import com.arpa.oms.service.IOmsSaleReturnItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * OMS销售退货明细 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2020-11-17
 */
@Service
public class OmsSaleReturnItemServiceImpl extends ServiceImpl<OmsSaleReturnItemMapper, OmsSaleReturnItem> implements IOmsSaleReturnItemService {

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(OmsSaleReturnItem entity) {
        if(StringUtils.isBlank(entity.getCode())){
            entity.setCode(IdUtil.simpleUUID());
        }
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
        }
        return super.save(entity);
    }

    /**
     * 根据code更新实体
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public int updateByCode(OmsSaleReturnItem entity) {
        if(StringUtils.isBlank(entity.getModifiedBy())){
            entity.setModifiedBy(UserUtil.getCode());
        }
        return baseMapper.update(entity,new QueryWrapper<OmsSaleReturnItem>().lambda().eq(OmsSaleReturnItem::getCode,entity.getCode()).eq(OmsSaleReturnItem::getGroupCode,UserUtil.getBranchCode()));
    }

    /**
     * 查询列表
     * @param omsSaleReturnItemDTO
     * @return
     */
    @Override
    public List<OmsSaleReturnItemVO> queryList(OmsSaleReturnItemDTO omsSaleReturnItemDTO){
        //排序字段名需要驼峰转数据库下划线类型字段名
        if(StringUtils.isNotEmpty(omsSaleReturnItemDTO.getSortField())){
            omsSaleReturnItemDTO.setSortField(CommonUtil.camel2Underline(omsSaleReturnItemDTO.getSortField()));
        }
        List<OmsSaleReturnItemVO> omsSaleReturnItemVOList = baseMapper.queryList(omsSaleReturnItemDTO);
        return omsSaleReturnItemVOList;
    }

    /**
     * 查询合计,包含总数
     * @param omsSaleReturnItemDTO
     * @return
     */
    @Override
    public OmsSaleReturnItemVO queryListSum(OmsSaleReturnItemDTO omsSaleReturnItemDTO){
        OmsSaleReturnItemVO omsSaleReturnItemVO = baseMapper.queryListSum(omsSaleReturnItemDTO);
        return omsSaleReturnItemVO;
    }

    /**
     * @param saleReturnCode :
     * @description 销售退单item根据出库单item更新出库数量
     * @author xuyang
     * @date 2020/12/14 17:35
     **/
    @Override
    public void updateOutQuantity(String saleReturnCode) {
        this.baseMapper.updateOutQuantity(saleReturnCode);
    }

    /**
     * @param itemDTO :
     * @description 关联销售明细表查询退货信息
     * @author xuyang
     * @date 2020/12/24 16:35
     **/
    @Override
    public List<OmsSaleReturnItemVO> queryListWithRefundInfo(OmsSaleReturnItemDTO itemDTO) {
        return this.baseMapper.queryListWithRefundInfo(itemDTO);
    }

}
